package com.tour.controller;

import com.tour.bean.*;
import com.tour.service.TicketinfoService;
import com.tour.service.UserTicketService;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName TicketinfoController
 * @Author 张梦兴
 * @Date 2020/3/17 16:25
 * @describe
 */
@RestController
@RequestMapping(value = "/Ticketinfo")
public class TicketinfoController {

    private final Logger log=Logger.getLogger(TicketinfoController.class);

    @Autowired
    private TicketinfoService ticketinfoService;
    @Autowired
    private UserTicketService userTicketService;



    /**
     * [查询] 根据主键 id 查询
     * @author
     * @date 2020/03/17
     *
     * @return*/
    @RequestMapping("/load")
    public Ticketinfo load(int id){
        return ticketinfoService.load(id);
    }

    /**
     * [查询] 分页查询
     * @author
     * @date 2020/03/17
     **/
    @RequestMapping("/pageList")
    public Map<String, Object> pageList(BaseParam baseParam) {
        log.debug(baseParam);
        JSONObject json = baseParam.getParamJson();
        int offset=json.getInt("offset");
        int pagesize=json.getInt("pagesize");
        String startStation=json.getString("startStation");
        String lastStation=json.getString("lastStation");
        String startTime=json.getString("startTime");
        return ticketinfoService.pageList(offset, pagesize,startStation,lastStation,startTime);
    }

    /**
     * [新增] 用户订购景点
     * @author
     * @date 2020/03/15
     **/
    @RequestMapping("/insert/{id}")
    public ReturnT<String> insert(@PathVariable("id") int id, HttpServletRequest request){
        Consumer userInfo = (Consumer) request.getSession().getAttribute("userInfo");
        if(null==userInfo){
            return new ReturnT<>(401, "请先登录！");
        }
        Map byid=userTicketService.getUserTicketById(userInfo.getId(), id);
        if(null!=byid){
            return new ReturnT<>(402, "已经存在！");
        }
        userTicketService.insertUserTicket(userInfo.getId(), id);
        return ReturnT.SUCCESS;
    }

}

